import com.hp.util.RedisUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:spring/applicationContext.xml")
public class RadisTestR1 {
    @Resource
    RedisUtil redis;

    /*String*/
    @Test
    public void test(){
       // Jedis jedis = new Jedis("127.0.0.1", 6379);
        redis.set("admin","张三");
        System.out.println(redis.get("admin"));
    }
    //String 有时效的字符串
    @Test
    public void test1(){
       redis.set("code","0367",10);
        System.out.println(redis.get("code"));
    }
    /*Hash哈希类型*/
    @Test
    public void test2(){
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.hset("username","1","张三");
        jedis.hset("username","2","李四");
        jedis.hset("username","3","王五");
        System.out.println(jedis.hget("username","1"));
        System.out.println(jedis.hgetAll("username"));
    }
    @Test
    public void test3(){
//        Jedis jedis = new Jedis("127.0.0.1", 6379);
//        //重名会报错类型不正确
//        jedis.hset("admin","username","张三");
//        jedis.hset("admin","sex","男");
//        jedis.hset("admin","age","20");
//        System.out.println(jedis.hget("admin","username"));
//        System.out.println(jedis.hgetAll("admin"));
        redis.hset("admin","username","张三");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sex","男");
        map.put("age","20");
        redis.hmset("admin",map);
        System.out.println(redis.hget("admin","username"));
        System.out.println(redis.hmget("admin"));
    }
    //list集合类型
    @Test
    public void test4(){
        redis.lSet("mylist","张三","李四","王五");
        redis.lSet("mylist","1","2","3");
        //Jedis jedis = new Jedis("127.0.0.1", 6379);
        //左插入
        //jedis.lpush("list","aaa","bbb","ccc");
        //右插入
        //jedis.rpush("list","ddd","eee","fff");
        //System.out.println(jedis.lrange("list",0,-1));
        System.out.println(redis.lGet("mylist",0,-1));
        System.out.println(redis.lRemove("mylist",0,"1"));
        System.out.println(redis.lGet("mylist",0,-1));
    }
    //set不能重复重复只输出一个
    @Test
    public void test5(){
//        Jedis jedis = new Jedis("127.0.0.1", 6379);
//        jedis.sadd("numbers","1","2","3","4");
//        System.out.println(jedis.smembers("numbers"));
        redis.sSet("numbers","1","2","3","4","5");
        System.out.println(redis.sGet("numbers"));
        redis.setRemove("numbers","3","5");
        System.out.println(redis.sGet("numbers"));
    }
    //zset
    @Test
    public void test6(){
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //有序自动排序
        jedis.zadd("demo",22,"张三");
        jedis.zadd("demo",88,"李四");
        jedis.zadd("demo",33,"王五");
        System.out.println(jedis.zrange("demo",0,-1));
    }
}
